<template>
  <div>
    <headImg></headImg>
    <div class="input-box">
      <div class="input-item register">
        <img class="phone-img-2" v-show="phoneNumber === ''" src="../../assets/img/login/phone-click.png">
        <img class="phone-img-2" v-show="phoneNumber !== ''" src="../../assets/img/login/phone.png">
        <input class="input-identify" v-model="phoneNumber" maxlength="11" type="tel" placeholder="请输入手机号">
        <img class="phone-clear identify-clear" @click="phoneClear" v-show="phoneNumber !== ''"
             src="../../assets/img/login/clear.png">
        <div class="getIdentify"  :class="[identify.isSending?'getIdentify-sending':'']"  @click="sendcode">{{btntxt}}</div>
       <!-- <button type="button" @click="testInterval" class="btns">{{btntxt}}</button>-->
      </div>
      <div class="input-item pwd">
        <img class="identify-img" v-show="identify.number === ''" src="../../assets/img/login/identify-click.png">
        <img class="identify-img" v-show="identify.number !== ''" src="../../assets/img/login/identify.png">
        <input class="input-phone" v-model="identify.number" placeholder="请输入验证码">
        <img class="phone-clear" @click="clearIdentify" v-show="identify.number !== ''"
             src="../../assets/img/login/clear.png">
      </div>
      <div class="input-button btn-small" @click="reg_next">下一步</div>
      <div class="other">
        <router-link id="login" to="/login"><span class="forget-register">返回登录>></span></router-link>
      </div>
    </div>
  </div>
</template>

<script>
import headImg from '../head'
import {Toast} from 'vue-ydui/dist/lib.rem/dialog'
import {SENDCHECKCODE, CHECKCODE} from '../../api/request'
export default {
  components: {headImg},
  name: 'register',
  action: '',
  data () {
    return {
      btntxt: '获取验证码',
      phoneNumber: '',
      identify: {
        number: '',
        isSending: false
      },
      time: 60,
      timeInterval: null
    }
  },
  mounted () {
    this.action = this.$route.params.action
  },
  methods: {
    phoneClear: function () {
      this.phoneNumber = ''
    },
    clearIdentify: function () {
      this.identify.number = ''
    },
    sendcode: function () {
      let reg = 11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/
      if (this.time.number !== this.time.limit) {
        return null
      }
      if (this.phoneNumber === '') {
        Toast({mes: '手机号不能为空', timeout: 1000})
        //  alert('手机号不能为空')
      } else if (!reg.test(this.phoneNumber)) {
        Toast({mes: '请输入正确的手机号', timeout: 1000})
      } else {
        this.timer()
      }
    },
    timer: function () {
      const that = this
      SENDCHECKCODE(that.phoneNumber, that.action).then(
        res => {
          if (res.data.code === 200) {
            that.identify.isSending = true
            that.btntxt = that.time + 's后重发'
            that.timeInterval = setInterval(function () {
              that.time--
              if (that.time > 0) {
                that.btntxt = that.time + 's后重发'
              } else {
                that.time = 60
                that.btntxt = '获取验证码'
                that.identify.isSending = false
                clearInterval(that.timeInterval)
                that.timeInterval = null
              }
            }, 1000)
          } else {
            console.log(res.data.msg)
            Toast({mes: res.data.msg, timeout: 1000})
          }
        },
        error => {
          console.log(error)
        })
    },
    reg_next: function () {
      let reg = 11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/
      if (this.phoneNumber === '' || this.identify.number === '') {
        //   alert('手机号或者验证码不能为空')
        Toast({mes: '手机号或者验证码不能为空', timeout: 1000})
      } else if (!reg.test(this.phoneNumber)) {
        Toast({mes: '请输入正确的手机号', timeout: 1000})
        //  alert('请输入正确的手机号')
      } else {
        CHECKCODE(this.phoneNumber, this.identify.number).then(
          res => {
            console.log(res.data)
            if (res.data.code === 200) {
              if (this.action === 'REGIST') {
                let nextUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?' +
                  'appid=wx1f4ed91c7207f884&' +
                  'redirect_uri=http%3a%2f%2fflctec.com%2f%23%2fsetPWD%2fREGIST' +
                  '&response_type=code&scope=snsapi_userinfo' +
                  '&state=' + this.phoneNumber + '#wechat_redirect'
                window.location.href = nextUrl
              } else {
                this.$store.dispatch('updateUserId', res.data.uid)
                this.$router.push({path: '/setPWD/' + this.action, query: {phone: this.phoneNumber}})
              }
            } else {
              console.log(res.data.code)
              Toast({mes: res.data.msg, timeout: 1000})
            }
          },
          error => {
            console.log(error)
          })
        //   this.$router.push({path: '/login'})
      }
    }
  }
}
</script>

<style scoped>
  .register {
    position: relative
  }

  .input-identify {
    width: 2.2rem;
    color: #848282;
    font-size: 0.3rem;
    line-height: 0.4rem;
    padding-left: 0.15rem;
  }

  .identify-clear {
    float: right;
    margin-right: 1.5rem;
    margin-top: 0.1rem;
  }

  .getIdentify {
    position: absolute;
    right: 0.1rem;
    top: 0;
    width: 1.26rem;
    height: 0.42rem;
    border-radius: 0.1rem;
    margin-bottom: 0.1rem;
    font-size: 0.16rem;
    border: solid #00c0f8 1px;
    color: #00c0f8;
    line-height: 0.42rem;
    text-align: center;
  }

  .getIdentify-sending {
    border: solid #999 1px;
    color: #999;
  }

  .btns {
    position: absolute;
    right: 0.1rem;
    top: 0;
    width: 1.16rem;
    height: 0.42rem;
    border-radius: 0.1rem;
    margin-bottom: 0.1rem;
    font-size: 0.2rem;
    border: solid #00c0f8 1px;
    color: #00c0f8;
    line-height: 0.38rem;
    text-align: center;
    background-color: #fff;
  }
</style>
